package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Entrepreneur;
import com.fsnbigdata.domain.Enterprise;
import com.fsnbigdata.domain.QEntrepreneur;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = EntrepreneurRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = EntrepreneurRepositoryCustom.class)
@Transactional(readOnly = true)
public class EntrepreneurRepositoryImpl extends QueryDslRepositorySupportExt<Entrepreneur> implements EntrepreneurRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    EntrepreneurRepositoryImpl() {
        super(Entrepreneur.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ENTERPRISE = "enterprise";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DESCRIPTION = "description";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String SEX = "sex";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated method documentation
     *
     * @param enterprise
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Entrepreneur> findByEnterprise(Enterprise enterprise, GlobalSearch globalSearch, Pageable pageable) {
        QEntrepreneur entrepreneur = QEntrepreneur.entrepreneur;
        JPQLQuery<Entrepreneur> query = from(entrepreneur);
        Assert.notNull(enterprise, "enterprise is required");
        query.where(entrepreneur.enterprise.eq(enterprise));
        Path<?>[] paths = new Path<?>[] { entrepreneur.enterprise, entrepreneur.name, entrepreneur.sex, entrepreneur.description };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ENTERPRISE, entrepreneur.enterprise).map(NAME, entrepreneur.name).map(SEX, entrepreneur.sex).map(DESCRIPTION, entrepreneur.description);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, entrepreneur);
    }
}
